/*
 * StrahlentherapieUISession.cc
 *
 *  Created on: Oct 26, 2010
 *      Author: pwagner
 */

#include "StrahlentherapieUISession.hh"

#include <iostream>
#include <fstream>
#include <ctime>
#include <string>
using namespace std;

StrahlentherapieUISession::StrahlentherapieUISession()
{
	G4cout << "Beginning to write logfiles ..." << G4endl;

	time_t secs = time(0);
	tm * timeinfo = localtime(&secs);
	char timeString[50];
	strftime(timeString, 50, "%y.%m.%d-%H:%M:%S", timeinfo);
	string path("log/");

	runLogFile.open((path + timeString + "_run.log").c_str(), ios::trunc | ios::out);
	errorLogFile.open((path + timeString + "_error.log").c_str(), ios::trunc | ios::out);
	headLogFile.open((path + timeString + "_head.log").c_str(), ios::trunc | ios::out);
	detectorPlateLogFile.open((path + timeString + "_detectorPlate.log").c_str(), ios::trunc | ios::out);
	energyHistLogFile.open((path + timeString + "_energyHist.log").c_str(), ios::trunc | ios::out);
}

StrahlentherapieUISession::~StrahlentherapieUISession()
{
	runLogFile.close();
	errorLogFile.close();
	headLogFile.close();
	detectorPlateLogFile.close();
	energyHistLogFile.close();
}

G4int StrahlentherapieUISession::ReceiveG4cout(G4String coutString)
{
	if(coutString.contains("Headbox\t"))
	{
		// ohne Prefix "Cell " ausgeben
		headLogFile << coutString.replace(0, 8, 0, 0) << flush;
	}
	else if(coutString.contains("DetectorPlate\t"))
	{
		detectorPlateLogFile << coutString.replace(0, 14, 0, 0) << flush;
	}
	else if(coutString.contains("ESHIST\t"))
	{
		energyHistLogFile << coutString.replace(0, 7, 0, 0) << flush;
	}
	else
	{
		runLogFile << coutString << flush;
		cout << coutString << flush;
	}
	return 0;
}

G4int StrahlentherapieUISession::ReceiveG4cerr(G4String cerrString)
{
	errorLogFile << cerrString << flush;
	cout << cerrString << flush;
	return 0;
}
